﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.Modules.Readers
{
    /// <summary>
    /// Interaction logic for DocReader.xaml
    /// </summary>
    public partial class DocReader : UserControl
    {
        public DocReader()
        {
            InitializeComponent();
        }

        public void DisplayContent(string content)
        {
            this.LoadText(this.flowDocReader, content);
        }

        private void LoadText(FlowDocumentReader flowDocumentReader, string content)
        {
            if (flowDocumentReader.Document != null)
            {
                FlowDocument flowDocument = flowDocumentReader.Document;
                flowDocument.Blocks.Clear();

                try
                {
                    //if (!content.StartsWith("<"))
                    //    content = "<p>" + content + "</p>";
                    string dataFormat = DataFormats.Rtf;
                   
                    if (dataFormat == DataFormats.Xaml)
                    {
                        using (MemoryStream msDocument = new MemoryStream((new ASCIIEncoding()).GetBytes(content)))
                        {
                            TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
                            textRange.Load(msDocument, DataFormats.Xaml);
                            flowDocumentReader.Document = flowDocument;
                        }
                    }
                    else
                    {
                        TextRange txtRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
                        MemoryStream memStream = new MemoryStream(Encoding.ASCII.GetBytes(content));
                        txtRange.Load(memStream, dataFormat);
                    }

                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                    Console.WriteLine(ex.ToString());
                }
            }
        }
    }
}
